package com.legrand.device.controller;



import com.legrand.core.config.RedisUtils;
import com.legrand.core.exception.CustomizeException;
import com.legrand.core.permission.RolePermission;
import com.legrand.device.entity.Teacher;
import com.legrand.device.feign.UserFeignServer;
import com.legrand.device.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author xiah
 * 2018-06-26 05:11:16
 * 该类用来测试 统一异常处理  切面权限控制
 */
@Controller
@RequestMapping("/api/worker")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private UserFeignServer userFeignServer;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 限流测试
     */
    @RequestMapping("/test")
    @ResponseBody
    public String test(@RequestBody Teacher teacher) throws Exception {
        //JwtToken.checkToken2("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ4aWFoIiwiY29tcGFueSI6ImxlZ3JhbmQiLCJleHAiOjE1MzA3Nzg4MTYsImlhdCI6MTUzMDc3ODcxNn0.wzlIoMkZAY9MTIR62ibVpXZs-NVNONM8G3hUpcngoXg");
        //return teacherService.test();
        /*ThreadLocal threadLocal=new ThreadLocal();
        threadLocal.set(1);
        System.out.println(threadLocal.get()); ;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String myValue = (String) requestAttributes.getAttribute("userName",RequestAttributes.SCOPE_REQUEST);
        String authHeader = (String) request.getAttribute("userName");
        return  authHeader;*/
        redisUtils.set("123", "hello world");
        System.out.println("进入了方法");
        String string = redisUtils.get("123").toString();
        return string;
    }

    /**
     * 限流测试
     */
    @RequestMapping("/rr")
    @ResponseBody
    public String test2(HttpServletRequest httpServletRequest) {
        String authorization = httpServletRequest.getHeader("Authorization");
        Enumeration ee = httpServletRequest.getHeaderNames();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String aa = request.getHeader("xxx");
        System.out.println("我不限制次数");
        return "我不限制次数";
    }

    @RequestMapping("/getTeacher")
    @ResponseBody
    public Teacher getTeacher() {
        return teacherService.getTeacher();
    }

    /**
     * @author xiah
     * 2018-06-26 05:09:59
     * 统一异常处理测试 抛出/by 0 错误
     */
    @RequestMapping("/getUserName")
    @ResponseBody
    public String getUserName() {
        Integer a = 1;
        Integer b = 0;
        throw new CustomizeException(111, "sdfsd");
        //Integer c = a / b;
        //return userFeignServer.getUserName();
    }

    /**
     * @author xiah
     * 2018-06-26 05:09:25
     * 切面权限测试
     */
    @RequestMapping("/rightTest")
    @RolePermission(roles = {"user"}, note = "能调用该接口的角色")
    @ResponseBody
    public String rightTest() {
        return "调用成功，拥有该权限";
    }


}
